package ${classPackage!};

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;

<#if classImports??>
<#list classImports as importItem>
import ${importItem};
</#list>
</#if>
import com.listeningframework.boot.autoconfigure.orm.jpa.search.SpecBuilder;
import com.listeningframework.boot.autoconfigure.orm.jpa.support.ListeningJpaServiceAdapter;
import com.listeningframework.boot.domains.Pagination;
import com.listeningframework.boot.domains.Paging;
import com.listeningframework.boot.exception.BusinessException;

/**
 * 
 * @description ${modelDesc!} Service
 *
 * @author Listening
 * @version $Id: ${modelPrefix!}Service.java, v 0.1 ${currentDateTime!} Listening Exp $
 */
@Component
public class ${modelPrefix!}Service extends ListeningJpaServiceAdapter<${modelPrefix!}${modelSuffix!}, Long> {
    /**
     * ${modelPrefix!} Repository
     */
    @Autowired
    private ${modelPrefix!}Repository ${modelPrefix?uncap_first}Repository;

    /** 
     * @see com.listeningframework.boot.autoconfigure.orm.jpa.support.ListeningJpaServiceAdapter#page(java.lang.Object, com.listeningframework.boot.domains.Paging)
     */
    @Override
    public Pagination<${modelPrefix!}${modelSuffix!}> page(${modelPrefix!}${modelSuffix!} entity, Paging paging) throws BusinessException {
        SpecBuilder builder = SpecBuilder.newinstance();
        // TODO 根据需要设置查询条件

        Specification<${modelPrefix!}${modelSuffix!}> spec = convertSpec(builder, ${modelPrefix!}${modelSuffix!}.class);
        PageRequest request = convertPageRequest(paging, Direction.DESC, "id");
        Page<${modelPrefix!}${modelSuffix!}> page = ${modelPrefix?uncap_first}Repository.findAll(spec, request);
        return convertPagination(page);
    }

    /** 
     * @see com.listeningframework.boot.autoconfigure.orm.jpa.support.ListeningJpaServiceAdapter#save(java.lang.Object)
     */
    @Override
    public ${modelPrefix!}${modelSuffix!} save(${modelPrefix!}${modelSuffix!} entity) throws BusinessException {
        Long recordId = entity.getId();
        // Step 1. 主键为空执行插入
        if (recordId == null) {
            return ${modelPrefix?uncap_first}Repository.save(entity);
        }
        // Step 2. 记录不存在抛异常无法更新, 存在则更新
        ${modelPrefix!}${modelSuffix!} dbEntity = ${modelPrefix?uncap_first}Repository.findOne(recordId);
        if (dbEntity == null) {
            throw new BusinessException("要更新的记录不存在");
        } else {
            copyUpdateFields(entity, dbEntity);
            return ${modelPrefix?uncap_first}Repository.save(dbEntity);
        }
    }

    /** 
     * @see com.listeningframework.boot.autoconfigure.orm.jpa.support.ListeningJpaServiceAdapter#delete(java.io.Serializable)
     */
    @Override
    public void delete(Long id) throws BusinessException {
        ${modelPrefix?uncap_first}Repository.delete(select(id));
    }

    /** 
     * @see com.listeningframework.boot.autoconfigure.orm.jpa.support.ListeningJpaServiceAdapter#select(java.io.Serializable)
     */
    @Override
    public ${modelPrefix!}${modelSuffix!} select(Long id) throws BusinessException {
        ${modelPrefix!}${modelSuffix!} entity = ${modelPrefix?uncap_first}Repository.findOne(id);
        if (entity == null) {
            throw new BusinessException("记录不存在");
        }
        return entity;
    }

    /** 
     * @see com.listeningframework.boot.autoconfigure.orm.jpa.support.ListeningJpaServiceAdapter#filter(java.lang.Object)
     */
    @Override
    public List<${modelPrefix!}${modelSuffix!}> filter(${modelPrefix!}${modelSuffix!} entity) throws BusinessException {
        SpecBuilder builder = SpecBuilder.newinstance();
        // TODO 根据需要设置个性化查询条件
        // builder.addFilter("_id", Operations.EQ, getBid());
        Specification<${modelPrefix!}${modelSuffix!}> spec = convertSpec(builder, ${modelPrefix!}${modelSuffix!}.class);
        return ${modelPrefix?uncap_first}Repository.findAll(spec);
    }

}
